package io.github.ali.commons.code;

import org.apache.commons.lang3.StringUtils;

/**
 * @author ALi
 * @version 1.0
 * @date 2021-10-15 14:19
 * @description
 */
public abstract class AbstractRespCode implements RespCode{
    public final String code;
    public final String message;

    protected AbstractRespCode(String code, String message) {
        this.code = code;
        this.message = message;
        fillDefaultCode();
    }
    protected AbstractRespCode(String serviceId, String subCode, String message) {
        this.code = makeCode(serviceId, subCode);
        this.message = message;
        fillDefaultCode();
    }

    /**
     * 重写此方法以自定义code生成规则
     * 默认系统ID+子码(少于三位左补0)
     * @param serviceId
     * @param subCode
     * @return
     */
    protected String makeCode(String serviceId, String subCode){
        return serviceId + StringUtils.leftPad(subCode, 3, "0");
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    private void fillDefaultCode(){
        Result.fillSuccessRespCodeAndFailedRespCode(getSuccess(), getFailed());
    }

    /**
     * 默认成功RespCode
     * @return
     */
    protected abstract RespCode getSuccess();

    /**
     * 默认失败RespCode
     * @return
     */
    protected abstract RespCode getFailed();
}
